﻿using MySql.Data.MySqlClient;

using NewChillers.Helpers;
using NewChillers.Models;
using NewChillers.Utils;
using NewChillers.Views;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NewChillers.ViewModels 
{
    public class ScanProductViewModel:ObservableObject
    {
        private ScanProductPage page;

        #region 属性
        private string _productSerialNumber;
        private bool _isEnabled01 = true;
        private bool _isEnabled02 = true;
        private bool _isEnabled03 = true;
        private bool _isEnabled04 = true;
        private bool _isEnabled05 = true;
        private bool _isEnabled06 = true;

        public string ProductSerialNumber
        {
            get { return _productSerialNumber; }
            set
            {
                _productSerialNumber = value;
                RaisePropertyChanged();
            }
        }

        public bool IsEnabled01
        {
            get { return _isEnabled01; }
            set
            {
                _isEnabled01 = value;
                RaisePropertyChanged();
            }
        }

        public bool IsEnabled02
        {
            get { return _isEnabled02; }
            set
            {
                _isEnabled02 = value;
                RaisePropertyChanged();
            }
        }

        public bool IsEnabled03
        {
            get { return _isEnabled03; }
            set
            {
                _isEnabled03 = value;
                RaisePropertyChanged();
            }
        }

        public bool IsEnabled04
        {
            get { return _isEnabled04; }
            set
            {
                _isEnabled04 = value;
                RaisePropertyChanged();
            }
        }

        public bool IsEnabled05
        {
            get { return _isEnabled05; }
            set
            {
                _isEnabled05 = value;
                RaisePropertyChanged();
            }
        }

        public bool IsEnabled06
        {
            get { return _isEnabled06; }
            set
            {
                _isEnabled06 = value;
                RaisePropertyChanged();
            }
        }


        public ICommand SelectDeviceCommand { get; set;}

        private ProductModel productModel;
        #endregion

        //构造函数
        public ScanProductViewModel(ScanProductPage page)
        {
            CheckButtonIsEnabled();

            ProductSerialNumber = GetProductSerialNumber();
            SelectDeviceCommand = new RelayCommand<string>(ExecuteSelectDevice);
            productModel = new ProductModel();

            this.page = page;
        }

        /// <summary>
        /// 检查按钮是否可用
        /// </summary>
        private void CheckButtonIsEnabled()
        {
            IsEnabled01 = DeviceDataStore.IsEnableWorkstation_01;
            IsEnabled02 = DeviceDataStore.IsEnableWorkstation_02;
            IsEnabled03 = DeviceDataStore.IsEnableWorkstation_03;
            IsEnabled04 = DeviceDataStore.IsEnableWorkstation_04;
            IsEnabled05 = DeviceDataStore.IsEnableWorkstation_05;
            IsEnabled06 = DeviceDataStore.IsEnableWorkstation_06;
        }

        /// <summary>
        /// 选择设备
        /// </summary>
        /// <param name="obj"></param>
        private void ExecuteSelectDevice(string obj)
        {
            if (int.TryParse(obj, out int workstationId))
            {
                //禁用按钮防止再次选择
                DisableWorkstationButton(workstationId);

                productModel.WorkstationId = workstationId;
                productModel.ProductSN = ProductSerialNumber;
                productModel.ProcessStatus = 0;
                productModel.StartTime = DateTime.Now;
                //加入数据库
                InsertDb(productModel);


                //关闭页面
                page.Close();
            }
            else
            {
                // 转换失败的处理
                MsgBoxHelper.Warning("无效的设备编号");
                LogHelper.Warn(this.GetType(), $"[参数转换失败]无法将 '{obj}' 转换为整数");
            }
        }

        /// <summary>
        /// 禁用设备按钮
        /// </summary>
        /// <param name="workstationId">工位ID</param>
        private void DisableWorkstationButton(int workstationId)
        {
            switch (workstationId)
            {
                case 1:
                    DeviceDataStore.IsEnableWorkstation_01 = false;
                    break;
                case 2:
                    DeviceDataStore.IsEnableWorkstation_02 = false;
                    break;
                case 3:
                    DeviceDataStore.IsEnableWorkstation_03 = false;
                    break;
                case 4:
                    DeviceDataStore.IsEnableWorkstation_04 = false;
                    break;
                case 5:
                    DeviceDataStore.IsEnableWorkstation_05 = false;
                    break;
                case 6:
                    DeviceDataStore.IsEnableWorkstation_06 = false;
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 往数据库里加入数据
        /// </summary>
        /// <param name="productModel"></param>
        private void InsertDb(ProductModel productModel)
        {
            try
            {
                // 使用MySQL帮助类执行插入操作
                string sql = @"INSERT INTO product_info
                      (workstation_id, product_sn, process_status, start_time) 
                      VALUES 
                      (@workstationId, @productSN, @processStatus, @startTime)";

                // 创建参数
                var parameters = new MySqlParameter[]
                {
                    new MySqlParameter("@workstationId", productModel.WorkstationId),
                    new MySqlParameter("@productSN", productModel.ProductSN),
                    new MySqlParameter("@processStatus", productModel.ProcessStatus),
                    new MySqlParameter("@startTime", productModel.StartTime)
                };

                // 执行插入操作
                int rowsAffected = MysqlHelper.ExecuteNonQuery(sql, parameters);
               
            }
            catch (MySqlException ex)
            {
                MsgBoxHelper.Error($"数据库操作失败: {ex.Message}");
            }
            catch (Exception ex)
            {
                MsgBoxHelper.Error($"操作失败: {ex.Message}");
            }
        }

        //获取产品编号
        private string GetProductSerialNumber()
        {
            return DeviceDataStore.ScanValue_real;
        }
    }
}
